﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
//using KeepoMVC.Models.Services.Types;

namespace Keepo.Models.Services
{
    public class DesignTemplateExport
    {
        public DesignTemplateExport() { }

        public int DesignTemplateId { get; set; }

        public String Title { get; set; }
        public String Description { get; set; }
        public String GUID { get; set; }
        public String XmlURL { get; set; }
        public int CategoryId { get; set; }
        public bool IsRendered { get; set; }
        public int BackgroundRgb { get; set; }
        public string BackgroundRgbHex { get; set; }
        public bool IsHighlighted { get; set; }
        public int DesignableProductId { get; set; }
        public string ModifiedDate { get; set; }
        public int Views { get; set; }

        public TemplatePageExport[] Pages { get; set; }


        public DesignTemplateExport(DesignTemplate template)
        {
            DesignTemplateId = template.DesignTemplateId;
            Title = template.Title;
            Description = template.Description;
            GUID = template.GUID;
            XmlURL = template.URL;
            CategoryId = template.TemplateCategoryId;
            IsRendered = template.IsRendered;
            BackgroundRgb = template.BackgroundRgb;
            BackgroundRgbHex = "#" + template.BackgroundRgb.ToString("X6");
            IsHighlighted = template.IsHighlighted;
            DesignableProductId = template.DesignableProductId ?? -1;
            ModifiedDate = template.ModifiedDate.ToShortDateString();
            Views = template.Views;

            TemplatePage[] templatePages = (from page in template.TemplatePages
                                            orderby page.PageIdx
                                            select page).ToArray();

            Pages = templatePages.Select(page => new TemplatePageExport(page)).ToArray();
        }

        public DesignTemplate toEntity()
        {
            DesignTemplate template = new DesignTemplate();

            template.TemplateCategoryId = this.CategoryId;
            template.Title = this.Title;
            template.Description = this.Description;
            template.GUID = this.GUID;
            template.URL = this.XmlURL;
            template.IsRendered = this.IsRendered;
            template.BackgroundRgb = this.BackgroundRgb;
            template.IsHighlighted = this.IsHighlighted;

            if(this.DesignableProductId > 0)
            {
                template.DesignableProductId = this.DesignableProductId;
            }

            if (!string.IsNullOrEmpty(this.ModifiedDate))
            {
                template.ModifiedDate = DateTime.Parse(this.ModifiedDate);
            }
            template.Views = this.Views;

            return template;
        }


    }
}